<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AgentController
 *
 * @author LeVinh
 */
include_once APPLICATION_PATH."/models/Store.php";
class Admin_StoreController extends Zend_Controller_Action {
    public function init() {
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        $this->model = new Model_Store();
        $this->modelMapper = new Model_StoreMapper();
    }
    public function indexAction() {}
    public function listAction() {}
     public function deleteAction(){ 
        $id = $this->_getParam("id","");
        $this->view->itemm=$id;
        $redirectUrl=$this->aConfig["site"]["url"]."admin/store/list";
        if(empty($id)){
            $this->_redirect($redirectUrl);
        }
        $item=$this->modelMapper->find($id,$this->model);
        $this->modelMapper->delete($id);
        $this->_redirect($redirectUrl);       
    }  
    public  function delconfirmAction()
    {
        $id_filter = $this->_getParam("id","0");
        $this->_helper->layout->disableLayout();
        $count =0;
        echo $count;
        exit();
    }    
    public function serviceAction() {
        $status_filter = $this->_getParam("status","0");
        $this->_helper->layout->disableLayout();
        foreach ($this->modelMapper->fetchAll() as $items ) 
        {                            
            if($status_filter=="1" && $items->getStatus()=="1")
            {    
                $menber[]=array(
                    'id_store'=>$items->getId(),
                    'name_store'=>$items->getName(),                                   
                    'address_store'=>$items->getAddress(),
                    'province_id_store'=>GlobalLib::getName("Province", $items->getProvince_Id(), "name"),
                    'district_id_store'=>  GlobalLib::getName("District", $items->getDistrict_Id(), "name"),
                    'representavie_store'=>$items->getRepresentavie(),
                    'phone_store'=>$items->getPhone(),
                    'area_id_store'=>  GlobalLib::getName("Area", $items->getArea_Id(), "name"),
                    'charges_store'=>$items->getCharges(),
                    'user_id_store'=>GlobalLib::getName("User", $items->getUser_Id(), "username"),
                    'order_store'=>  $items->getOrder() ,
                    'status_store'=>$items->getStatus()                   
                ); 
            }
            else if($status_filter=="0")
            {
                $menber[]=array(
                    'id_store'=>$items->getId(),
                    'name_store'=>$items->getName(),                                   
                    'address_store'=>$items->getAddress(),
                    'province_id_store'=>GlobalLib::getName("Province", $items->getProvince_Id(), "name"),
                    'district_id_store'=>  GlobalLib::getName("District", $items->getDistrict_Id(), "name"),
                    'representavie_store'=>$items->getRepresentavie(),
                    'phone_store'=>$items->getPhone(),
                    'area_id_store'=>  GlobalLib::getName("Area", $items->getArea_Id(), "name"),
                    'charges_store'=>$items->getCharges(),
                    'user_id_store'=>GlobalLib::getName("User", $items->getUser_Id(), "username"),
                    'order_store'=>  $items->getOrder() ,
                    'status_store'=>$items->getStatus()        
                );  
            } 
        }
        echo json_encode($menber);
        exit();
    }
    public function addAction()
    {
        if ($this->getRequest()->isPost()) {   
            $redirectUrl = $this->aConfig["site"]["url"] . "/admin/store/list";
            if (isset($_POST["id"])) {
                $this->model->setId($_POST["id"]);
            }
            if (isset($_POST["name_store"])) {
                $this->model->setName($_POST["name_store"]);
            }            
            if (isset($_POST["address_store"])) {
                $this->model->setAddress($_POST["address_store"]);
            }
            if (isset($_POST["province_id"])) {
                $this->model->setProvince_Id($_POST["province_id"]);
            }
            if (isset($_POST["district_id"])) {
                $this->model->setDistrict_Id($_POST["district_id"]);
            }
            if (isset($_POST["representavie_store"])) {
                $this->model->setRepresentavie($_POST["representavie_store"]);
            }
            if (isset($_POST["phone_store"])) {
                $this->model->setPhone($_POST["phone_store"]);
            }           
            if (isset($_POST["area_id"])) {
                $this->model->setArea_Id($_POST["area_id"]);
            }
//            if (isset($_POST["charges_store"])) {
//                $this->model->setName($_POST["charges"]);
//            }
            if (isset($_POST["user_id"])) {
                $this->model->setUser_Id($_POST["user_id"]);
            }
            if (isset($_POST["order_store"])) {
                $this->model->setOrder($_POST["order_store"]);
            }            
            if (isset($_POST["status"])) {
                $status = 1;
            } else {
                $status = 0;
            }            
            $this->model->setStatus($status);
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);
        }
        $this->view->item = $this->model;
        $province_id= $this->model->getProvince_Id();
        if(empty($province_id)){
            $province_id=0;
        }
        $this->view->provinceHTML= GlobalLib::getComboByProvince('province_id', 'province', 'id', 'name', 0, false, 'form-control', '', '', '', 'onchange="getDistrict(\'' . $this->aConfig["site"]["url"] . 'admin/district/getdistrict\',$(\'#province_id\').val(),\'district_id\',\'Xin Chờ Trong Giây Lát\');"');
        $this->view->districtHTML=GlobalLib::getComboByDistrict('district_id', 'district', 'id', 'name', 0, false, 'form-control', '', 'where province_id=' . $province_id);   
    }    
    public function editAction() {
        $id= $this->_getParam("id","");
        $redirectUrl=$this->aConfig["site"]["url"]."admin/store/list";
        if(empty($id)){
            $this->_redirect($redirectUrl);
        }
        $this->modelMapper->find($id,$this->model);
        $getId=$this->model->getId();
        if(empty($getId)){
             $this->_redirect($redirectUrl);
        }
        if($this->getRequest()->isPost()){
            if (isset($_POST["id"])) {
                $this->model->setId($_POST["id"]);
            }
            if (isset($_POST["name_store"])) {
                $this->model->setName($_POST["name_store"]);
            }            
            if (isset($_POST["address_store"])) {
                $this->model->setAddress($_POST["address_store"]);
            }
            if (isset($_POST["province_id"])) {
                $this->model->setProvince_Id($_POST["province_id"]);
            }
            if (isset($_POST["district_id"])) {
                $this->model->setDistrict_Id($_POST["district_id"]);
            }
            if (isset($_POST["representavie_store"])) {
                $this->model->setRepresentavie($_POST["representavie_store"]);
            }
            if (isset($_POST["phone_store"])) {
                $this->model->setPhone($_POST["phone_store"]);
            }           
            if (isset($_POST["area_id"])) {
                $this->model->setArea_Id($_POST["area_id"]);
            }
//            if (isset($_POST["charges_store"])) {
//                $this->model->setName($_POST["charges_store"]);
//            }
            if (isset($_POST["user_id"])) {
                $this->model->setUser_Id($_POST["user_id"]);
            }
            if (isset($_POST["order_store"])) {
                $this->model->setOrder($_POST["order"]);
            }            
            if (isset($_POST["status"])) {
                $status = 1;
            } else {
                $status = 0;
            }            
            $this->model->setStatus($status);
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);
        }
        $this->view->item = $this->model;
        $province_id= $this->model->getProvince_Id();
        if(empty($province_id)){
            $province_id=0;
        }
        $this->view->provinceHTML= GlobalLib::getComboByProvince('province_id', 'province', 'id', 'name', 0, false, 'form-control', '', '', '', 'onchange="getDistrict(\'' . $this->aConfig["site"]["url"] . 'admin/district/getdistrict\',$(\'#province_id\').val(),\'district_id\',\'Xin Chờ Trong Giây Lát\');"');
        $this->view->districtHTML=GlobalLib::getComboByDistrict('district_id', 'district', 'id', 'name', 0, false, 'form-control', '', 'where province_id=' . $province_id);   
    }
}
